﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class PerchaseSubEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                    ReqPerchaseSub reqPerchaseSub = ctx.ReqPerchaseSubs.Single(r => r.Id == int.Parse(id));
                    GoodsName.Value = reqPerchaseSub.GoodsName;
                    PerPrice.Value = reqPerchaseSub.PerPrice.ToString();
                    Model.Value = reqPerchaseSub.Model;
                    Specification.Value = reqPerchaseSub.Specification;
                    Unit.Value = reqPerchaseSub.Unit;
                    Number.Value = reqPerchaseSub.Number.ToString();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            ReqPerchaseSub reqPerchaseSub;
            string id = Request.QueryString["id"];
            if (string.IsNullOrEmpty(id))
            {
                reqPerchaseSub = new ReqPerchaseSub();
                reqPerchaseSub.FormNo = Request.QueryString["formNo"];
                ctx.ReqPerchaseSubs.InsertOnSubmit(reqPerchaseSub);
            }
            else
            {
                reqPerchaseSub = ctx.ReqPerchaseSubs.Single(r => r.Id == int.Parse(id));
            }
            reqPerchaseSub.GoodsName = GoodsName.Value;
            reqPerchaseSub.PerPrice = decimal.Parse(PerPrice.Value);
            reqPerchaseSub.Model = Model.Value;
            reqPerchaseSub.Specification = Specification.Value;
            reqPerchaseSub.Unit = Unit.Value;
            reqPerchaseSub.Number = int.Parse(Number.Value);
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}